<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
    

    

    



    <meta charset="utf-8">
    
    
    <meta name="sogou_site_verification" content="true">
    
    
    
    <title>我所用到的Java8 | Lvshen&#39;s Blog | This is My World</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="Java8,Stream">
    <meta name="baidu-site-verification" content="VIVNdSiMZm">
    <meta name="description" content="进入新公司几个月，陆陆续续使用到了Java8的新技术，其中流使用的最多，在处理集合方面非常方便，下面是是我工作中常用到的Java8的功能。">
<meta name="keywords" content="Java8,Stream">
<meta property="og:type" content="article">
<meta property="og:title" content="我所用到的Java8">
<meta property="og:url" content="https://lvshen9.gitee.io/2019/07/06/1/index.html">
<meta property="og:site_name" content="Lvshen&#39;s Blog">
<meta property="og:description" content="进入新公司几个月，陆陆续续使用到了Java8的新技术，其中流使用的最多，在处理集合方面非常方便，下面是是我工作中常用到的Java8的功能。">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-07-06T07:57:33.170Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="我所用到的Java8">
<meta name="twitter:description" content="进入新公司几个月，陆陆续续使用到了Java8的新技术，其中流使用的最多，在处理集合方面非常方便，下面是是我工作中常用到的Java8的功能。">
    
    <link rel="shortcut icon" href="/img/mylogo.jpg">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">我的技术小房间</h5>
          <a href="mailto:https://lvshen9.github.io" title="https://lvshen9.github.io" class="mail">https://lvshen9.github.io</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/about"  >
                <i class="icon icon-lg icon-address-book"></i>
                About
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/collection"  >
                <i class="icon icon-lg icon-apple"></i>
                Collection
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://lvshen9.github.io/" target="_blank" >
                <i class="icon icon-lg icon-wordpress"></i>
                Blog
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/lvshen9" target="_blank" >
                <i class="icon icon-lg icon-github-alt"></i>
                GitHub
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">我所用到的Java8</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">我所用到的Java8</h1>
        <h5 class="subtitle">
            
                <time datetime="2019-07-06T04:16:22.000Z" itemprop="datePublished" class="page-time">
  2019-07-06
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/技术/">技术</a></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Lambda表达式和forEach循环"><span class="post-toc-number">1.</span> <span class="post-toc-text">Lambda表达式和forEach循环</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#集合创建"><span class="post-toc-number">2.</span> <span class="post-toc-text">集合创建</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Java8常用的集合操作"><span class="post-toc-number">3.</span> <span class="post-toc-text">Java8常用的集合操作</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Spring创建对象"><span class="post-toc-number">4.</span> <span class="post-toc-text">Spring创建对象</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#BigDecimal使用注意"><span class="post-toc-number">5.</span> <span class="post-toc-text">BigDecimal使用注意</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#使用SecureRandom"><span class="post-toc-number">6.</span> <span class="post-toc-text">使用SecureRandom</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#Java8-List转Map"><span class="post-toc-number">7.</span> <span class="post-toc-text">Java8 List转Map</span></a></li></ol>
        </nav>
    </aside>


<article id="post-1"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">我所用到的Java8</h1>
        <div class="post-meta">
            <time class="post-time" title="2019-07-06 12:16:22" datetime="2019-07-06T04:16:22.000Z"  itemprop="datePublished">2019-07-06</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/技术/">技术</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <p>进入新公司几个月，陆陆续续使用到了Java8的新技术，其中流使用的最多，在处理集合方面非常方便，下面是是我工作中常用到的Java8的功能。</p>
<a id="more"></a>
<h4 id="Lambda表达式和forEach循环"><a href="#Lambda表达式和forEach循环" class="headerlink" title="Lambda表达式和forEach循环"></a>Lambda表达式和forEach循环</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (CollectionUtils.isNotEmpty(prizes)) &#123;</span><br><span class="line">   prizes.forEach(x -&gt; &#123;</span><br><span class="line">      <span class="keyword">if</span> (PrizeType.BOOK.equals(x.getPrizeType())) &#123;</span><br><span class="line">          books.add(getByActivityId(x.getPrizeId()));</span><br><span class="line">      &#125;</span><br><span class="line">       &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>当循环遍历的数据很多时（1000条以上），Java8中的for循环新能要强于普通的for循环。</p>
<h4 id="集合创建"><a href="#集合创建" class="headerlink" title="集合创建"></a>集合创建</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (CollectionUtils.isEmpty(memberList)) &#123;</span><br><span class="line">    <span class="keyword">return</span> ImmutableList.of();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>使用Guava的<code>ImmutableList.of()</code>创建的是不可变集合，这样有利于节省空间。</p>
<h4 id="Java8常用的集合操作"><a href="#Java8常用的集合操作" class="headerlink" title="Java8常用的集合操作"></a>Java8常用的集合操作</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//filter过滤</span></span><br><span class="line">List&lt;RedeemActivity&gt; redeemActivitiesOfPrize = activities.stream()</span><br><span class="line">      .filter(x -&gt; activityIdsOfPrize.contains(x.getId())).collect(Collectors.toList());</span><br><span class="line"></span><br><span class="line"><span class="comment">//使用流降序排序</span></span><br><span class="line">userCOS.stream()             .sorted(Comparator.comparing(userCO::getCreateDate).reversed()).collect(Collectors.toList());</span><br><span class="line"></span><br><span class="line"><span class="comment">//通过昵称获取userId列表</span></span><br><span class="line"> List&lt;String&gt; userIds = users.stream().collect(() -&gt; <span class="keyword">new</span> ArrayList&lt;String&gt;(), (list, user) -&gt; list.add(user.getId()), (list1, list2) -&gt; list1.addAll(list2));</span><br><span class="line"></span><br><span class="line"> List&lt;String&gt; orderNos = orderStatus.stream().map(OrderDetail::getOrderNo).collect(toList());</span><br><span class="line"></span><br><span class="line"><span class="comment">//Optional.ofNullable使用</span></span><br><span class="line">List&lt;CommodityActivityRule&gt; excludedCommodityActivityRules = Optional.ofNullable(commodityActivityRules)</span><br><span class="line">				.orElseGet(ArrayList::<span class="keyword">new</span>).stream()</span><br><span class="line">				<span class="comment">// 排除当前活动</span></span><br><span class="line">				.filter(commodityActivityRule -&gt; !commodityActivityRule.getActivity().getId().equals(activityId))</span><br><span class="line">				.collect(Collectors.toList());</span><br><span class="line"><span class="comment">//对象不为null时使用of,对象可能为null也可能不为null时用ofNullable</span></span><br><span class="line"><span class="comment">//orElseGet:对象不为null不会重新创建；orElse：对象为不为null也会重新创建</span></span><br><span class="line"> Optional.of(marketingConfigRepository.listAll(<span class="keyword">new</span> MarketingConfig())).orElseGet(ArrayList::<span class="keyword">new</span>).stream()</span><br><span class="line">				.filter(marketingConfig -&gt; excludedMarketingConfigTypes.contains(marketingConfig.getType()))</span><br><span class="line">				.map(MarketingConfig::getId).collect(Collectors.toList());</span><br><span class="line"><span class="comment">//返回对象的时候可以用</span></span><br><span class="line">Optional.ofNullable(usageRules).orElseGet(ArrayList::<span class="keyword">new</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">//BigDecimal累加</span></span><br><span class="line">BigDecimal commodityAmount = orderDetails.stream().map(OrderDetail::getCommodityAmount).reduce(BigDecimal.ZERO, BigDecimal::add);</span><br><span class="line"></span><br><span class="line"><span class="comment">//判断Optional里面是否有值</span></span><br><span class="line">Optional&lt;GradeThresholdInfoCO&gt; nextOptional = gradeThresholdList.stream().filter(x -&gt; x.getLevel() != <span class="keyword">null</span> &amp;&amp; x.getLevel() == level + <span class="number">1</span>).findFirst();</span><br><span class="line">        <span class="keyword">if</span> (nextOptional.isPresent()) &#123;</span><br><span class="line">            <span class="keyword">return</span> nextOptional.get();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//findFirst()</span></span><br><span class="line">List&lt;Cookie&gt; targetCookies = Optional.ofNullable(cookies).orElseGet(ArrayList::<span class="keyword">new</span>).stream()</span><br><span class="line">				.filter(e -&gt; <span class="string">"jsessionid"</span>.equalsIgnoreCase(e.getName())).collect(Collectors.toList());</span><br><span class="line"></span><br><span class="line">		log.info(<span class="string">"Cookie名称为：&#123;&#125;的值为:&#123;&#125;"</span>, <span class="string">"jsessionid"</span>,</span><br><span class="line">				Optional.ofNullable(targetCookies).orElseGet(ArrayList::<span class="keyword">new</span>).get(<span class="number">0</span>).getValue());</span><br><span class="line"></span><br><span class="line">		Optional&lt;Cookie&gt; first = Optional.ofNullable(cookies).orElseGet(ArrayList::<span class="keyword">new</span>).stream()</span><br><span class="line">				.filter(e -&gt; <span class="string">"jsessionid"</span>.equalsIgnoreCase(e.getName())).findFirst();</span><br><span class="line">		<span class="keyword">if</span> (first.isPresent()) &#123;</span><br><span class="line">			log.info(<span class="string">"Cookie名称为：&#123;&#125;的值为:&#123;&#125;"</span>, <span class="string">"jsessionid"</span>,</span><br><span class="line">					first.get().getValue());</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//流大小比较</span></span><br><span class="line">Optional&lt;Optional&lt;Integer&gt;&gt; maxLevelOptional = ruleList.stream().map(x -&gt; x.getEarningRuleMap().keySet().stream().max((o1, o2) -&gt; o1.compareTo(o2))).max((o1, o2) -&gt; &#123;</span><br><span class="line">            Integer value1 = o1.isPresent() ?  o1.get() : <span class="number">0</span>;</span><br><span class="line">            Integer value2 = o2.isPresent() ?  o2.get() : <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">return</span> value1.compareTo(value2);</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">//日期获取最值（最大，最小）</span></span><br><span class="line">Optional&lt;ActivityVO&gt; max = activityVOS.stream().max(Comparator.comparing(ActivityVO::getEndDate));</span><br><span class="line">Optional&lt;ActivityVO&gt; min = activityVOS.stream().min(Comparator.comparing(ActivityVO::getStartDate));</span><br><span class="line">Date maxEndDate = max.get().getEndDate();</span><br><span class="line">Date minStartDate = min.get().getStartDate();</span><br><span class="line"></span><br><span class="line"><span class="comment">//groupingBy分组1</span></span><br><span class="line"> Map&lt;PrizeType, List&lt;RedeemActivityPrize&gt;&gt; prizeTypeAndPrizesMap = redeemActivityPrize.stream().collect(Collectors.groupingBy(RedeemActivityPrize::getPrizeType));</span><br><span class="line">        List&lt;RedeemActivityPrize&gt; couponPrizes = prizeTypeAndPrizesMap.get(PrizeType.COUPON);</span><br><span class="line">        List&lt;RedeemActivityPrize&gt; presentPrizes = prizeTypeAndPrizesMap.get(PrizeType.PRESENT);</span><br><span class="line"></span><br><span class="line"><span class="comment">//groupingBy分组2</span></span><br><span class="line">prizes.stream().collect(Collectors.groupingBy(RedeemActivityPrize::getPrizeId))</span><br><span class="line">				.forEach((prizeId, activityPrizes) -&gt; &#123;</span><br><span class="line">					List&lt;String&gt; activityIdsOfPrize = activityPrizes.stream()</span><br><span class="line">							.map(RedeemActivityPrize::getRedeemActivityId).collect(Collectors.toList());</span><br><span class="line"></span><br><span class="line">					List&lt;RedeemActivity&gt; redeemActivitiesOfPrize = activities.stream()</span><br><span class="line">							.filter(x -&gt; activityIdsOfPrize.contains(x.getId())).collect(Collectors.toList());</span><br><span class="line"></span><br><span class="line">					List&lt;RedeemActivityBriefCO&gt; briefCOS = ModelConvertorHelper.convertList(redeemActivitiesOfPrize,</span><br><span class="line">							RedeemActivityBriefCO<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line"></span><br><span class="line">					RedeemUsedPresentCO co = <span class="keyword">new</span> RedeemUsedPresentCO();</span><br><span class="line">					co.setPresentId(prizeId);</span><br><span class="line">					co.setActivities(briefCOS);</span><br><span class="line">					result.add(co);</span><br><span class="line">				&#125;);</span><br><span class="line"><span class="comment">//Java8 Function函数的使用</span></span><br><span class="line"><span class="comment">//T-入参类型，R-出参类型 </span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">static</span> &lt;T, R&gt; <span class="function">List&lt;R&gt; <span class="title">transform</span><span class="params">(List&lt;T&gt; list, Function&lt;T, R&gt; fx)</span> </span>&#123;</span><br><span class="line">        List&lt;R&gt; result = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">        <span class="keyword">for</span> (T element : list) &#123;</span><br><span class="line">            result.add(fx.apply(element));</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">  &#125;</span><br><span class="line">List&lt;Integer&gt; namesLength = transform(names, String::length);</span><br><span class="line"></span><br><span class="line"><span class="comment">//集合添加..</span></span><br><span class="line">Stream.of(task1, task2, task3, task4, task5).collect(toList())</span><br><span class="line"></span><br><span class="line"><span class="comment">//显示前几位的集合数据</span></span><br><span class="line">tasks.stream().filter(task -&gt; task.getType() == TaskType.READING).sorted(comparing(Task::getCreatedOn)).map(Task::getTitle).</span><br><span class="line">limit(n).collect(toList());</span><br><span class="line"></span><br><span class="line"><span class="comment">//Java8循环遍历</span></span><br><span class="line">couponIds.forEach(x -&gt; &#123;</span><br><span class="line">    RedeemActivityPrize presentRedeem = <span class="keyword">new</span> RedeemActivityPrize();</span><br><span class="line">    presentRedeem.setRedeemActivityId(redeemActivityId);</span><br><span class="line">    presentRedeem.setPrizeType(PrizeType.COUPON);</span><br><span class="line">    presentRedeem.setPrizeId(x);</span><br><span class="line">    redeemActivityPrizeList.add(presentRedeem);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h4 id="Spring创建对象"><a href="#Spring创建对象" class="headerlink" title="Spring创建对象"></a>Spring创建对象</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">static</span> ActivityRepository redeemActivityRepository = SpringContextHolder</span><br><span class="line">        .getBean(ActivityRepository<span class="class">.<span class="keyword">class</span>)</span>;</span><br></pre></td></tr></table></figure>
<p>除了@Autowired生成对象，Spring其他生成对象的方式。</p>
<h4 id="BigDecimal使用注意"><a href="#BigDecimal使用注意" class="headerlink" title="BigDecimal使用注意"></a>BigDecimal使用注意</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">你可能认为java中用new BigDecimal(0.1)创建的BigDecimal应该等于0.1（一个是1的无精度的值，一个是有精度的值），但实际上精确的是等于0.1000000000000000055511151231257827021181583404541015625。这是因为0.1不能被double精确的表示（下面大概描述一下原理）。因此，传入构造函数的值不是精确的等于0.1。</span><br><span class="line"></span><br><span class="line">当遇到需要涉及到精确计算的时候，如上面代码所示，要注意该构造函数是一个精确的转换，它无法得到与先调用Double.toString(double)方法将double转换成String，再使用BigDecimal(String)构造函数一样的结果。如果要达到这种结果，应该使用new BigDecimal(String value) 或 BigDecimal.valueof(double value)</span><br></pre></td></tr></table></figure>
<h4 id="使用SecureRandom"><a href="#使用SecureRandom" class="headerlink" title="使用SecureRandom"></a>使用SecureRandom</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">由于java.util.Random类依赖于伪随机数生成器，因此该类和相关的java.lang.Math.random（）方法不应用于安全关键应用程序或保护敏感数据。 在这种情况下，应该使用依赖于加密强随机数生成器（RNG）的java.security.SecureRandom类。</span><br><span class="line"></span><br><span class="line">PRNG(伪随机数)：</span><br><span class="line">伪随机数， 计算机不能生成真正的随机数，而是通用一定的方法来模拟随机数。伪随机数有一部分遵守一定的规律，另一部分不遵守任何规律。</span><br><span class="line"></span><br><span class="line">RNG(随机数)：</span><br><span class="line">随机数是由“随机种子”产生的，“随机种子”是一个无符号整形数。</span><br><span class="line"></span><br><span class="line">反例：</span><br><span class="line">Random random = new Random(); // Questionable use of Random</span><br><span class="line">byte bytes[] = new byte[20];</span><br><span class="line">random.nextBytes(bytes);</span><br><span class="line">正例：</span><br><span class="line">SecureRandom random = new SecureRandom(); </span><br><span class="line">byte bytes[] = new byte[20];</span><br></pre></td></tr></table></figure>
<h4 id="Java8-List转Map"><a href="#Java8-List转Map" class="headerlink" title="Java8 List转Map"></a>Java8 List转Map</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//1.这里有个实体对象(员工对象)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Employee</span> </span>&#123;</span><br><span class="line">    <span class="comment">// member variables</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> empId;</span><br><span class="line">    <span class="keyword">private</span> String empName;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> empAge;</span><br><span class="line">    <span class="keyword">private</span> String empDesignation;</span><br><span class="line"><span class="comment">//2.比如式样员工对象的empId作为key，值是员工姓名</span></span><br><span class="line"> Map&lt;Integer, String&gt; mapOfEmployees = employees.stream().collect(</span><br><span class="line">        Collectors.toMap(e -&gt; e.getEmpId(),e -&gt; e.getEmpName()));</span><br><span class="line"><span class="comment">//3.Map的Key是empId，整个对象为Map的值,但如果List中有重复的empId，映射到Map时，Key时不能重复的</span></span><br><span class="line"> Map&lt;Integer, String&gt; mapOfEmployees = employees.stream().collect(</span><br><span class="line">     Collectors.toMap(</span><br><span class="line">         e -&gt; e.getEmpId(), </span><br><span class="line">         e -&gt; e.getEmpName(), </span><br><span class="line">         (e1, e2) -&gt; e1 )); <span class="comment">// Merge Function</span></span><br><span class="line"><span class="comment">//4.将List转换为Map - 使用TreeMap对键进行自然排序</span></span><br><span class="line"> Map&lt;Integer, String&gt; mapOfEmployees = employees.stream().collect(</span><br><span class="line">                Collectors.toMap(</span><br><span class="line">                        e -&gt; e.getEmpId(), </span><br><span class="line">                        e -&gt; e.getEmpName(), </span><br><span class="line">                        (e1, e2) -&gt; e1 , <span class="comment">// Merge Function</span></span><br><span class="line">                        TreeMap&lt;Integer, String&gt;::<span class="keyword">new</span>)); <span class="comment">// Map Supplier</span></span><br><span class="line"><span class="comment">//如果你的TreeMap实现需要加入比较器，将上面代码中TreeMap&lt;Integer, String&gt;::new替换成：() -&gt; new TreeMap&lt;Integer, String&gt;(new MyComparator())</span></span><br></pre></td></tr></table></figure>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2019-07-06T07:57:33.170Z" itemprop="dateUpdated">2019-07-06 15:57:33</time>
</span><br>


        
        原文链接：<a href="/2019/07/06/1/" target="_blank" rel="external">https://lvshen9.gitee.io/2019/07/06/1/</a>
        
    </div>
    
    <footer>
        <a href="https://lvshen9.gitee.io">
            <img src="/img/avatar.jpg" alt="我的技术小房间">
            我的技术小房间
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Java8/">Java8</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Stream/">Stream</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://lvshen9.gitee.io/2019/07/06/1/&title=《我所用到的Java8》 — Lvshen's Blog&pic=https://lvshen9.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://lvshen9.gitee.io/2019/07/06/1/&title=《我所用到的Java8》 — Lvshen's Blog&source=进入新公司几个月，陆陆续续使用到了Java8的新技术，其中流使用的最多，在处理集合方面非常方便，下面是是我工作中常用到的Java8的功能。" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://lvshen9.gitee.io/2019/07/06/1/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《我所用到的Java8》 — Lvshen's Blog&url=https://lvshen9.gitee.io/2019/07/06/1/&via=https://lvshen9.gitee.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://lvshen9.gitee.io/2019/07/06/1/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2019/08/16/1/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">我所用到的Java8-续</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2019/05/26/1/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">MySQL为什么选择B+Tree做索引</h4>
      </a>
    </div>
  
</nav>



    











    <!-- Valine Comments -->
    <div class="comments vcomment" id="comments"></div>
    <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
    <script src="//unpkg.com/valine@latest/dist/Valine.min.js"></script>
    <!-- Valine Comments script -->
    <script>
        var GUEST_INFO = ['nick','mail','link'];
        var guest_info = 'nick,mail,link'.split(',').filter(function(item){
          return GUEST_INFO.indexOf(item) > -1
        });
        new Valine({
            el: '#comments',
            notify: 'false' == 'true',
            verify: 'false' == 'true',
            appId: "dy9kXHwg5jQUlLryQmpjWRlM-gzGzoHsz",
            appKey: "P9Nh39Ol0JbMMiYqNGHEP3ml",
            avatar: "mm",
            placeholder: "Just go go",
            guest_info: guest_info.length == 0 ? GUEST_INFO : guest_info,
            pageSize: "10"
        })
    </script>
    <!-- Valine Comments end -->







</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="https://lvshen9.github.io/blog2/pay/weixin.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="https://lvshen9.github.io/blog2/pay/weixin.jpg" data-alipay="https://lvshen9.github.io/blog2/pay/zhifu.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
            <span>博客内容遵循 <a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>我的技术小房间 &copy; 2015 - 2020</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://lvshen9.gitee.io/2019/07/06/1/&title=《我所用到的Java8》 — Lvshen's Blog&pic=https://lvshen9.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://lvshen9.gitee.io/2019/07/06/1/&title=《我所用到的Java8》 — Lvshen's Blog&source=进入新公司几个月，陆陆续续使用到了Java8的新技术，其中流使用的最多，在处理集合方面非常方便，下面是是我工作中常用到的Java8的功能。" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://lvshen9.gitee.io/2019/07/06/1/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《我所用到的Java8》 — Lvshen's Blog&url=https://lvshen9.gitee.io/2019/07/06/1/&via=https://lvshen9.gitee.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://lvshen9.gitee.io/2019/07/06/1/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=https://lvshen9.gitee.io/2019/07/06/1/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
